package bsf.baseservice.businessmq.dal;

import bsf.baseservice.businessmq.model.tb_producter_model;
import bsf.db.DbConn;
import bsf.util.ConvertUtil;

import java.util.List;
import java.util.Map;

public class tb_producter_dal
{
	public  boolean deleteNotOnLineByMqpathid(DbConn PubConn, int mqpathid, int maxtimeoutsenconds)
	{
			String Sql = "delete from tb_producter where mqpathid=? and DATEDIFF(S,lastheartbeat,getdate())>?";
			int rev = PubConn.executeSql(Sql, new Object[]{mqpathid,maxtimeoutsenconds});
			return true;
	}

	public  boolean deleteClient(DbConn PubConn, int mqpathid, long tempid)
	{
			String Sql = "delete from tb_producter where mqpathid=? and tempid=?";
			int rev = PubConn.executeSql(Sql,new Object[]{mqpathid,tempid});
			return true;
	}

	public  boolean clientHeatbeat(DbConn PubConn, int mqpathid, long tempid)
	{
			String Sql = "update tb_producter WITH (ROWLOCK) set lastheartbeat=getdate() where mqpathid=? and tempid=?";
			int rev = PubConn.executeSql(Sql,new Object[]{mqpathid,tempid});
			return true;
	}


	public boolean add2(DbConn PubConn, tb_producter_model model)
	{
				//��������ʱid(������������Ψһ,Guidתlong)
				//����������
				//ip��ַ
				//����id
				////�������������ʱ��
				//new ProcedureParameter("@lastheartbeat",    model.lastheartbeat),
				////�����ߴ���ʱ��
				//new ProcedureParameter("@createtime",    model.createtime)
			int rev = PubConn.executeSql("insert into tb_producter(tempid,productername,ip,mqpathid,lastheartbeat,createtime)" +
					" values(?,?,?,?,getdate(),getdate())", new Object[]{model.tempid,model.productername,model.ip,model.mqpathid});
			return rev == 1;

	}

	public tb_producter_model get(DbConn PubConn, long tempid, int mqpathid)
	{
			StringBuilder stringSql = new StringBuilder();
			stringSql.append("select s.* from tb_producter s WITH(NOLOCK) where s.tempid=? and mqpathid=?");
			List<Map<String,Object>> ds = PubConn.executeList(stringSql.toString(), new Object[]{tempid,mqpathid});
			if (ds != null && ds.size() > 0)
			{
				return createModel(ds.get(0));
			}
			return null;
	}



	public tb_producter_model createModel(Map<String,Object> dr)
	{
		tb_producter_model o = new tb_producter_model();

		//
		if(dr.containsKey("id"))
		{
			o.id = ConvertUtil.objToInt(dr.get("id"));
		}
		//��������ʱid(������������Ψһ,Guidתlong)
		if(dr.containsKey("tempid"))
		{
			o.tempid= ConvertUtil.objToInt64(dr.get("tempid"));
		}
		//����������
		if(dr.containsKey("productername"))
		{
			o.productername= ConvertUtil.nullToStr(dr.get("productername"));
		}
		//ip��ַ
		if(dr.containsKey("ip"))
		{
			o.ip= ConvertUtil.nullToStr(dr.get("ip"));
		}
		//����id
		if(dr.containsKey("mqpathid"))
		{
			o.mqpathid= ConvertUtil.objToInt(dr.get("mqpathid"));
		}
		//�������������ʱ��
		if(dr.containsKey("lastheartbeat"))
		{
			o.lastheartbeat= ConvertUtil.objToDateTime(dr.get("lastheartbeat"));
		}
		//�����ߴ���ʱ��
		if(dr.containsKey("createtime"))
		{
			o.createtime= ConvertUtil.objToDateTime(dr.get("createtime"));
		}
		return o;
	}
}